﻿Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Workflow.ComponentModel
Imports System.Workflow.ComponentModel.Compiler
Imports System.Workflow.ComponentModel.Serialization
Imports System.Workflow.Runtime.DebugEngine

Namespace System.Workflow.Activities
    <SRDisplayName("CodeConditionDisplayName"), ToolboxItem(False), ActivityValidator(GetType(CodeConditionValidator))> _
    Public Class CodeCondition
        Inherits ActivityCondition
        ' Events
        <MergableProperty(False), SRDescription("ExpressionDescr"), SRCategory("Handlers")> _
        Public Custom Event Condition As EventHandler([Of] ConditionalEventArgs)
            AddHandler(ByVal value As EventHandler([Of] ConditionalEventArgs))
                MyBase.AddHandler(CodeCondition.ConditionEvent, value)
            End AddHandler
            RemoveHandler(ByVal value As EventHandler([Of] ConditionalEventArgs))
                MyBase.RemoveHandler(CodeCondition.ConditionEvent, value)
            End RemoveHandler
        End Event

        ' Methods
        Public Overrides Function Evaluate(ByVal ownerActivity As Activity, ByVal provider As IServiceProvider) As Boolean
            If (provider Is Nothing) Then
                Throw New ArgumentNullException("provider")
            End If
            Dim e As New ConditionalEventArgs
            Dim invocationList As EventHandler([Of] ConditionalEventArgs)() = MyBase.GetInvocationList([Of] EventHandler([Of] ConditionalEventArgs))(CodeCondition.ConditionEvent)
            Dim service As IWorkflowDebuggerService = [TryCast](provider.GetService(GetType(IWorkflowDebuggerService)),IWorkflowDebuggerService)
            If (Not invocationList Is Nothing) Then
                Dim handler As EventHandler([Of] ConditionalEventArgs)
                For Each handler In invocationList
                    If (Not service Is Nothing) Then
                        service.NotifyHandlerInvoking(handler)
                    End If
                    handler.Invoke(ownerActivity, e)
                    If (Not service Is Nothing) Then
                        service.NotifyHandlerInvoked
                    End If
                Next
            End If
            Return e.Result
        End Function

        Protected Overrides Function GetBoundValue(ByVal bind As ActivityBind, ByVal targetType As Type) As Object
            If (bind Is Nothing) Then
                Throw New ArgumentNullException("bind")
            End If
            If (targetType Is Nothing) Then
                Throw New ArgumentNullException("targetType")
            End If
            Dim runtimeValue As Object = bind
            Dim parentDependencyObject As Activity = [TryCast](MyBase.ParentDependencyObject,Activity)
            If (Not parentDependencyObject Is Nothing) Then
                runtimeValue = bind.GetRuntimeValue(parentDependencyObject, targetType)
            End If
            Return runtimeValue
        End Function


        ' Fields
        Public Shared ReadOnly ConditionEvent As DependencyProperty = DependencyProperty.Register("Condition", GetType(EventHandler([Of] ConditionalEventArgs)), GetType(CodeCondition))

        ' Nested Types
        Private Class CodeConditionValidator
            Inherits ConditionValidator
            ' Methods
            Public Overrides Function Validate(ByVal manager As ValidationManager, ByVal obj As Object) As ValidationErrorCollection
                Dim errors As New ValidationErrorCollection
                errors.AddRange(MyBase.Validate(manager, obj))
                Dim condition As CodeCondition = [TryCast](obj,CodeCondition)
                If (((Not condition Is Nothing) AndAlso (condition.GetInvocationList([Of] EventHandler([Of] ConditionalEventArgs))(CodeCondition.ConditionEvent).Length = 0)) AndAlso (condition.GetBinding(CodeCondition.ConditionEvent) Is Nothing)) Then
                    Dim hashtable As Hashtable = [TryCast](condition.GetValue(WorkflowMarkupSerializer.EventsProperty),Hashtable)
                    If ((Not hashtable Is Nothing) AndAlso (Not hashtable.Item("Condition") Is Nothing)) Then
                        Return errors
                    End If
                    errors.Add(ValidationError.GetNotSetValidationError((MyBase.GetFullPropertyName(manager) & ".Condition")))
                End If
                Return errors
            End Function

        End Class
    End Class
End Namespace

